package io.github.talelin.latticy.service.impl.ag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.talelin.latticy.dto.ag.room.CreateOrUpdateRoomDTO;
import io.github.talelin.latticy.mapper.ag.RoomMapper;
import io.github.talelin.latticy.mapper.ag.RoomSelectMapper;
import io.github.talelin.latticy.model.ag.RoomDO;
import io.github.talelin.latticy.model.ag.RoomDetailDO;
import io.github.talelin.latticy.service.ag.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RoomSelectMapper selectMapper;

    @Override
    public List<RoomDetailDO> getRooms() {
        QueryWrapper<RoomDetailDO> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("update_time");
        return selectMapper.selectList(wrapper);
    }

    @Override
    public List<RoomDetailDO> getRoomsByBuildingId(int building_id) {
        QueryWrapper<RoomDetailDO> wrapper = new QueryWrapper<>();
        wrapper.select("id","building_id","name","belong_building","floor","manager","phone").eq("building_id", building_id);
        return selectMapper.selectList(wrapper);
    }

    @Override
    public RoomDO getById(int id) {
        return roomMapper.selectById(id);
    }

    @Override
    public boolean createRoom(CreateOrUpdateRoomDTO validator) {
        RoomDO room = new RoomDO();
        room.setBuildingId(validator.getBuilding_id());
        room.setName(validator.getName());
        room.setManager(validator.getManager());
        room.setFloor(validator.getFloor());
        room.setPhone(validator.getPhone());
        return roomMapper.insert(room) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return roomMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateRoom(RoomDO room, CreateOrUpdateRoomDTO validator) {
        room.setBuildingId(validator.getBuilding_id());
        room.setName(validator.getName());
        room.setFloor(validator.getFloor());
        room.setManager(validator.getManager());
        room.setPhone(validator.getPhone());
        return roomMapper.updateById(room) > 0;
    }

    @Override
    public IPage<RoomDetailDO> getRoomByPage(Page<RoomDetailDO> pager) {
        QueryWrapper<RoomDetailDO> wrapper = new QueryWrapper<>();
        wrapper.select().orderByDesc("update_time");
        IPage<RoomDetailDO> iPage =selectMapper.selectPage(pager, wrapper);
        return iPage;
    }

    @Override
    public IPage<RoomDetailDO> searchRoomByKeyword(Page<RoomDetailDO> pager, String keyword) {
        QueryWrapper<RoomDetailDO> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyword).or()
                .like("floor", keyword).or()
                .like("belong_building", keyword);
        IPage<RoomDetailDO> iPage = selectMapper.selectPage(pager, wrapper);
        return iPage;
    }
}
